package com.putable.nmc.dmv;

import com.putable.nmc.world.XRandom;
import com.putable.pqueue.PQueue;


public class GoodTeller extends Teller {
	
	XRandom rand = new XRandom(Seed.seed);
	public GoodTeller(PQueue pq, CustomerLine cl) {
		super(pq);
		setState(TellerState.WAITING);
		run(0.0,cl);
	}

	@Override
	public void run(double time, CustomerLine cLine){
		if(getState() == TellerState.WAITING || getState() == TellerState.CLEANING){
			if(cLine.isEmpty()){
				//If the line is empty clean
				setState(TellerState.CLEANING);
				this.setTimeOfNextTask(time + rand.nextExponential(20));
				this.getPQueue().insert(this);
			}else{
				//Serve the next customer
				setState(TellerState.SERVING);
				CLineAble c = this.serve(cLine, this.getPQueue(), time);
				this.setTimeOfNextTask(time+c.getCit().getWillTakeTime());
				this.getPQueue().insert(this);
			}
		}else if(getState() == TellerState.SERVING){
			this.setTimeOfNextTask(time+rand.nextExponential(1));
			setState(TellerState.WAITING);
			this.getPQueue().insert(this);
		}else {
			System.err.println("Invalid State");
		}
		
	}

	@Override
	public CLineAble serve(CustomerLine cLine, PQueue pq, double time) {
		CLineAble c = cLine.remove();
		pq.delete(c);
		c.setTimeOfNextTask(time+c.getCit().getWillTakeTime());
		c.setState(CustomerState.TRANSACTING);
		c.getPQueue().insert(c);
		return c;
	}
}
